package com.langsin.chenxinhao;

import cn.hutool.core.lang.hash.Hash;
import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.chenxinhao.mapper.GoodsMapper;
import com.langsin.chenxinhao.mapper.TeacherMapper;
import com.langsin.chenxinhao.mapper.UserMapper;
import com.langsin.chenxinhao.mapper.WorkerMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class MyTest {
    SqlSession sqlSession = null;


    @Test
    public void testVO() throws Exception {
        QueryVo queryVo = new QueryVo();
        queryVo.setRoleCode("plain");
        queryVo.setAddress("济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryByRoleAndAddress(queryVo);
        users.forEach(System.out::println);
    }

    @Test
    public void testMap() throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        map.put("roleCode","plain");
        map.put("address","济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryUserByMap(map);
        users.forEach(System.out::println);
    }

    @Test
    public void testTeacher() throws Exception {
        int tid = 1009;
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.queryByID(tid);
        System.out.println(teacher);
    }

    @Test
    public void testUser() throws Exception {
        int uid = 702;
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);
    }

    @Test
    public void tableName() throws Exception {
        String tableName = "goods";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tableName);
        System.out.println(counts);

    }

    @Test
    public void testMapperLike2() throws Exception {
        String name = "X";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        List<Goods> goods = goodsMapper.queryByName2(name);
        goods.forEach(System.out::println);
    }


    @Test
    public void testMapperLike() throws Exception {
        String name = "%C%";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        List<Goods> goods = goodsMapper.queryByName(name);
        goods.forEach(System.out::println);

    }

    @Test
    public void testMapperGoods() throws Exception {
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods = new Goods();
        goods.setGoodsName("玫瑰花");
        goods.setShopPrice(100.99);
        goodsMapper.addGoods(goods);
        System.out.println(goods);
    }

    @Test
    public void testMapper3() throws Exception {
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        Worker worker = new Worker();
        worker.setWname("警察");
        worker.setSex("男");
        worker.setAge(30);
        int row = workerMapper.insertWorker2(worker);
        System.out.println(row);
        System.out.println(worker);

    }

    @Test
    public void testMapper2() throws Exception {
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        Worker worker = new Worker();
        // 主键  整数自增 （字符串uuid，雪花算法）
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("xxx");
        worker.setSex("女");
        worker.setAge(30);
        int row = workerMapper.insertWorker(worker);
        System.out.println(row);
        System.out.println(worker);

    }

    @Test
    public void testMapper1() throws Exception {
        // 获取 mapper接口的 动态代理
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        List<Worker> workers = workerMapper.selectByAge(50);
        workers.forEach(System.out::println);

    }

    // 每次单元测试之前执行
    @Before
    public void before() throws Exception {
        // 读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        // 工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        // 获取一个会话连接，默认不自动提交
        sqlSession = sqlSessionFactory.openSession();
    }

    // 每次单元测试之后执行
    @After
    public void after() {
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test1() throws Exception {

        //查询一个worker "4792a048-5e5d-11e9-9dc0-28d244d68b7c"
        Worker worker = sqlSession.selectOne("worker.selectWorker", "4792a048-5e5d-11e9-9dc0-28d244d68b7c");
        System.out.println(worker);

//        演示修改
//        Worker worker = new Worker();
//        worker.setAge(500);
//        worker.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
//
//        int row = sqlSession.update("worker.updateAgeByID", worker);
//        System.out.println(row);


    }
}
